package com.example.keshe0337.biz;

import android.content.Context;

import com.example.keshe0337.dao.ParkingRecordDao;
import com.example.keshe0337.dao.ParkingSpaceDao;
import com.example.keshe0337.dao.RentDao;
import com.example.keshe0337.database.AppDatabase;
import com.example.keshe0337.entity.ParkingRecord;
import com.example.keshe0337.entity.ParkingSpace;
import com.example.keshe0337.entity.Rent;
import com.example.keshe0337.utils.Calculation;
import com.example.keshe0337.utils.Checker;
import com.example.keshe0337.utils.TimeConverters;

import java.util.ArrayList;
import java.util.List;

public class RecordService {

    private final ParkingRecordDao recordDao;
    private final ParkingSpaceDao spaceDao;
    private final RentDao rentDao;
    public RecordService(Context context) {
        AppDatabase db = AppDatabase.getInstance(context);
        this.recordDao = db.parkingRecordDao();
        this.spaceDao = db.parkingSpaceDao();
        this.rentDao = db.rentDao();
    }

    public int startRent(Long uId, Long sId, String vehiclePlate) {


        if (!Checker.isValidPlate(vehiclePlate)) {
            return -1;//车牌格式错误
        }

        ParkingSpace psById = spaceDao.getPSById(sId);
        if (psById == null){
            return -2;//车位不存在
        }

        if (!psById.isStatus()) {
            return -3;//车位已占用
        }
        ParkingRecord pr = new ParkingRecord();
        pr.setUserId(uId);  // 假设 user 的 ID 是 1
        pr.setSpaceId(sId); // 假设 space 的 ID 是 1
        pr.setVehiclePlate(vehiclePlate);

        pr.setStartTime(TimeConverters.getNewTime());
        pr.setEndTime(0L);
        pr.setFee(0.0);

        //设置车位已占用并开始记录
        psById.setStatus(false);
        spaceDao.updatePS(psById);
        //保存起始记录
        long rId = recordDao.insertPR(pr);
        //保存租借信息
        Rent rent = new Rent();
        rent.setRecordId(rId);
        rent.setStartTime(TimeConverters.format(pr.getStartTime()));
        rent.setSpaceNum(psById.getSpaceNum());
        rent.setUserId(pr.getUserId());
        rentDao.insertRent(rent);
        return 0;
    }

    public int endRent(Long rentId){
        Rent rentById = rentDao.getRentById(rentId);
        if (rentById == null) {
            return 1;//记录为空
        }
        ParkingRecord prById = recordDao.getPRById(rentById.getRecordId());
        if (prById == null) {
            return 1;//记录为空
        }
        ParkingSpace psById = spaceDao.getPSById(prById.getSpaceId());
        if (psById == null) {
            return 2;//车位信息缺失
        }
        psById.setStatus(true);
        spaceDao.updatePS(psById); //恢复车位可租借状态
        prById.setEndTime(TimeConverters.getNewTime()+1000*60*30);//开始结算
        prById.setTotalTime(TimeConverters.calculate(prById.getStartTime(),prById.getEndTime()));
        prById.setFee(Calculation.settlement(prById.getTotalTime(),psById.getHourlyFee()));
        prById.setSpaceNum(psById.getSpaceNum());
        recordDao.updatePR(prById);
        rentDao.deleteRent(rentById);
        return 0;
    }

    public List<ParkingRecord> getAllPRs() {
        return recordDao.getAllPRs();
    }

    public List<ParkingRecord> getPRsByUid(Long id) {
        List<ParkingRecord> result = recordDao.getPRByUId(id);
        return result != null ? result : new ArrayList<>();
    }


    public ParkingRecord getPRById(Long id){
        return recordDao.getPRById(id);
    }

    public void deletePRById(Long id) {
        ParkingRecord prById = recordDao.getPRById(id);
        recordDao.deletePR(prById);
    }

}
